package com.fg.cloud.manager.service.factory.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.fg.cloud.common.CommonConstants;
import com.fg.cloud.common.PageUtils;
import com.fg.cloud.common.encrypt.Encrypt;
import com.fg.cloud.common.finals.Agency;
import com.fg.cloud.manager.entry.BaseEntity;
import com.fg.cloud.manager.entry.factory.FactoryEntity;
import com.fg.cloud.manager.entry.factory.FactoryUserEntity;
import com.fg.cloud.manager.entry.factory.vo.FactoryVo;
import com.fg.cloud.manager.mapper.BaseMapper;
import com.fg.cloud.manager.mapper.agency.AgencyInfoMapper;
import com.fg.cloud.manager.mapper.agency.AgencyProductMapper;
import com.fg.cloud.manager.mapper.factory.FactoryMapper;
import com.fg.cloud.manager.mapper.factory.FactoryUserMapper;
import com.fg.cloud.manager.service.BaseServiceImpl;
import com.fg.cloud.manager.service.factory.FactoryService;

/**
 * Description:厂商基本信息 Service-impl
 * 
 * @author shaoqiping
 * @date 2017年8月3日上午9:24:45
 */
@Service
public class FactoryServiceImpl extends BaseServiceImpl implements FactoryService {

	@Autowired
	private FactoryMapper factoryMapper;
	@Autowired
	private AgencyProductMapper system_agencyProductMapper;	
	@Autowired
	private FactoryUserMapper factoryUserMapper;
	@Autowired
	private AgencyInfoMapper agencyInfoMapper;

	@Override
	protected <T extends BaseEntity> BaseMapper getBaseMapper() {
		return factoryMapper;
	}
	
	@Override
	public boolean deleteInitData(Long facId) {
		//1.删除基础资料
			//1.1 删除厂商参数
			long r1 = factoryMapper.delFacParam(facId);
			//1.2 删除厂商仓库
			long r2 = factoryMapper.delFacWarehouse(facId);
			//1.3 删除厂商角色
			long temp = 0;
			long r3 = 0;
			temp += factoryMapper.delFacUserRole(facId);
			temp += factoryMapper.delFacRoleMenu(facId);
			if ( temp == 2 ) r3 = factoryMapper.delFacRole(facId);
			//1.4 删除厂商物流信息
			long r4 = factoryMapper.delFacLogistics(facId);
		//2.删除辅助资料
			//2.1 删除产品类别
			long r5 = factoryMapper.delFacCategory(facId);
			//2.2 删除单位信息
			long r6 = factoryMapper.delFacUnit(facId);
			//2.3 删除商品规格
			temp = 0;
			long r7 = 0;
			temp += factoryMapper.delSpecValue(facId);
			temp += factoryMapper.delSpecKey(facId);
			if ( temp == 2 ) r7 = factoryMapper.delSpecModel(facId);
			//2.4 删除经销商区域
			long r8 = agencyInfoMapper.delAgencyArea(facId);
			//2.5 删除经销商等级
			long r9 = agencyInfoMapper.delAgencyLevel(facId);
			//2.6删除新闻公告
			long r10 = factoryMapper.delFacNews(facId);
			
		if ( r1>0 && r2>0 && r3>0 && r4>0 && r5>0 && r6>0 && r7>0 && r8>0 && r9>0 && r10>0 ) return true;
		return false;
	}

	@Override
	public FactoryEntity findByFacName(String facName) {
		return factoryMapper.selectByFacName(facName);
	}
	
	@Override
	public boolean findByNameId(String facName, Long id) {		
		return factoryMapper.selectByNameId(facName, id) > 0 ? true : false;
	}

	@Override
	@Transactional
	public boolean insertSelective(FactoryVo factoryEntity) {
		int temp = factoryMapper.insertSelective(factoryEntity);
		temp += factoryMapper.insertSysParam1(factoryEntity.getId());
		temp += factoryMapper.insertSysParam2(factoryEntity.getId());
		temp += factoryMapper.insertSysParam3(factoryEntity.getId());
		temp += factoryMapper.insertSysParam4(factoryEntity.getId());
		FactoryUserEntity user = new FactoryUserEntity();
		user.setUserName(factoryEntity.getFacName());
		user.setUserType(1);
		user.setPassword(Encrypt.md5("123456"));
		user.setStatus(1);
		user.setFacId(factoryEntity.getId());
		factoryUserMapper.insertSelective(user);
		factoryUserMapper.batchInsert(user.getId(), new Long[]{1L});
		// 初始化厂商对应的基础数据
		/*AgencyAreaEntity area = new AgencyAreaEntity();
		area.setNumber("0");
		area.setName("全国");
		area.setPid(0L);
		area.setSeq(0);
		area.setStatus(1);
		area.setFacId(factoryEntity.getId());
		area.setLongName("全国");
		system_areaMapper.insert(area);
		// 初始化商品分类
		ProductCategoryEntity category = new ProductCategoryEntity();
		category.setFacId(factoryEntity.getId());
		category.setName("全部");
		category.setNumber("0");
		category.setPid(0L);
		category.setSeq(0);
		system_categoryMapper.insert(category);*/
		// 初始化权限表
		system_agencyProductMapper.selectTableExist(CommonConstants.DATABASE, Agency.getTableName(factoryEntity.getId()));

		return temp > 0 ? true : false;
	}

	/**
	 * 分页查询所有厂商基本信息
	 */
	@Override
	public void selectPage(PageUtils<FactoryEntity> pageinfo) {
		long total = factoryMapper.filterCount(pageinfo.getCondition());
		pageinfo.setTotal(total);
		if ( total > 0 ) {
			List<FactoryEntity> facEntities = factoryMapper.findList(pageinfo);
			pageinfo.setData(facEntities);
		}
	}

	/**
	 * 批量删除数据
	 */
	@Override
	@Transactional
	public boolean deletes(Long[] ids) {
		return factoryMapper.deletes(ids) > 0 ? true : false;
	}

	



}
